// pages/image_zy/image_zy.js
import {
    get_pc_img,
    get_pc_imgtype,
    get_avatar_img
} from '../../api/shool/get_image.js'
import {
    get_show
  } from "../../api/shool/get_score.js";
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
const app= getApp()
Page({
    //加载完毕
    image_load(){
        this.setData({
            is_end:true
        })
    },

    /**
     * 页面的初始数据
     */
    onChange(event) {
       
        this.data.image_list = []
        this.data.page = 1
        this.data.type = event.detail.name
        console.log(event.detail.name)
        
        this.setData({
            image_list: [],  
            active:event.detail.name,   
            is_end:false                 
           
        })
       
        if(this.data.type_shebei==2){
           
             this.get_image_phone(this.data.page, this.data.type)
        }else if(this.data.type_shebei==1){
           
                this.get_pc_img(this.data.type)
        }else{
            this.setData({
                avatar_list: [],  
                         
               
            })
            this.get_avatar(this.data.page,this.data.type)
        }
       
    },


    data: {
        loading: true,
        type_shebei:2,
        page: 1,
        active: 0,
        is_end:false,
        type_1: [{
                name: '热门精选',
                key: 0
            },
            {
                name: '明星',
                key: 136
            },
            {
                name: '体育',
                key: 41
            },
            {
                name: '影视',
                key: 44
            },
            {
                name: '美女',
                key: 10000
            },
            {
                name: '动漫',
                key: 49
            },

            {
                name: '网络流行',
                key: 37
            },
            {
                name: '个性',
                key: 47
            },
            {
                name: '明星美图',
                key: 58
            },
            {
                name: '文字背景',
                key: 39
            },
            {
                name: '学习',
                key: 51
            },
            {
                name: '清新',
                key: 50
            },
            {
                name: '聊天背景',
                key: 190
            },
            {
                name: '城市',
                key: 17
            },
            {
                name: '动物',
                key: 38
            },
            {
                name: '海滩',
                key: 53
            },
            {
                name: '暗黑',
                key: 2
            },
            {
                name: '游戏',
                key: 36
            },
            {
                name: '星空',
                key: 42
            },
            {
                name: '体育',
                key: 41
            },
            {
                name: '情感',
                key: 82
            },
            {
                name: '汽车',
                key: 43
            },
        ],
        type: 0,
 
        type_3: [{
                name: '热门精选',
                key: 0
            },
            {
                name: '网络流行',
                key: 37
            },
            
            {
                name: '小姐姐',
                key: 10000
            },
            {
                name: '动漫',
                key: 49
            },

           
            {
                name: '影视',
                key: 44
            },
            {
                name: '个性',
                key: 47
            },
            {
                name: '明星美图',
                key: 58
            },
            {
                name: '文字背景',
                key: 39
            },
            {
                name: '学习',
                key: 51
            },
            {
                name: '清新',
                key: 50
            },
            {
                name: '聊天背景',
                key: 190
            },
            {
                name: '城市',
                key: 17
            },
            {
                name: '动物',
                key: 38
            },
            {
                name: '明星',
                key: 136
            },
            {
                name: '体育',
                key: 41
            },
            {
                name: '海滩',
                key: 53
            },
            {
                name: '暗黑',
                key: 2
            },
            {
                name: '游戏',
                key: 36
            },
            {
                name: '星空',
                key: 42
            },
            {
                name: '体育',
                key: 41
            },
            {
                name: '情感',
                key: 82
            },
            {
                name: '汽车',
                key: 43
            },
        ],
        type_2: [
            {
                name: '动漫区',
                key: 26

            },
            {
                name: '游戏壁纸',
                key: 5
            },
            {
                name: '小姐姐',
                key: 6
            },
           
            {
                name: '4k专区',
                key: 36
            }
        ],
        type_4: [
            {name: '动漫',key:1},
            {name: '古风',key:2},
            {name: '情侣',key:3},
            {name: '男生',key:4},
            {name: '女生',key:5},
            {name: '明星',key:6},
            {name: '精选',key:8},
           
        ],
        image_list: [

        ],
        avatar_list:[]
    },
    //切换壁纸类型
    chant_type(e) {
       
        this.setData({
            image_list:[],
            is_end:false 
        })
        let type = e.currentTarget.dataset.type
        this.setData({ 
            type_shebei: type,
           
        })
        if (type == 1) {
            this.data.type_shebei = 1
            this.data.type=26
         
        } else if(type == 2) {
            this.data.type_shebei = 2
            this.data.type=0     
        }else{
            this.data.type_shebei = 3
            this.data.type=1
        }


        if (this.data.type_shebei == 1) {
           
           
            this.setData({
                type_1: this.data.type_2,       
                active:26,           
            })
            this.get_pc_img(this.data.type)
        } else if(this.data.type_shebei == 2) {
            this.setData({
                type_1: this.data.type_3,
              
            })
            this.get_image_phone(this.data.page, this.data.type)
            setTimeout(() => {
                this.setData({
                    
                    active:0, 
                })
            },500)
        }else{
            this.setData({
                type_1: this.data.type_4,
                active:1, 
            })
            this.data.page=0
           this. get_avatar(this.data.page, this.data.type)
        }

    },

   
    //获取头像资源
    get_avatar(page,type) {
        let that = this
        get_avatar_img({
            type:type,
            page:page
        }).then(res=>{
            this.setData({
                avatar_list: [...this.data.avatar_list,...res.data.data.list]
            })
        })
        // wx.request({
        //     url:`https://spare.maibaapp.com/bbs/newUgc/category/info/${2}/${type}/${18*page}/${17+(18*page)}`,
        //     success(res) {
               
        //     }
        // })
    },



    //获取pc图片分类列表
    get_pc_img_type() {
        get_pc_imgtype().then(res => {
           


            let arr = []
            for (let i in res.data.data) {
               
            }


        })
    },
    //获取pc图片
    get_pc_img(id) {
        get_pc_img({
            cid:id
        }).then(res => {
            if(res.data.statusCode==200){
                //截取数组
                let arr = res.data.data
                let title_arr=[]
               arr= arr.map(item => {
                   
                   if(item.title.length!=0){
                    title_arr  =item.title[0].split(',')
                   //去掉空字符
                     title_arr=title_arr.filter(item => {
                            return item != ''
                        })
                   }else{
                     title_arr =[]
                   }
                    return {
                        title:title_arr,
                        url:item.url,
                    }
                })
                this.setData({
                    image_list:arr
                })
            }
        })
    },

    

    //图片预览
    yulan(e) {
let that = this
        if (!wx.getStorageSync('token')) {
            Dialog.confirm({
                title: '您还未登录哦',
                message: '是否现在登录',
              })
              .then(() => {
                wx.navigateTo({
                  url: '/pages/login/login?url=/pages/image_zy/image_zy',
  
                })
              })
              .catch(() => {
                // on cancel
              });
  
  
          } else{
             
         let url = e.currentTarget.dataset.url.card||e.currentTarget.dataset.url
            
        let arr=[]
        if(this.data.type_shebei==1){
            arr=this.data.image_list.map(item=>{
                return item.url
            })
        }else if(this.data.type_shebei==2){
            arr=this.data.image_list
        } else{
            console.log(e)
            arr.push(e.currentTarget.dataset.url.card)
            arr.push(e.currentTarget.dataset.url.avatar)
            wx.setClipboardData({
                data:e.currentTarget.dataset.url.memo ,
                success(res) {
                  //粘贴到对应的位置
                    wx.showToast({title:'文案已复制到剪切板'})
                }
              })
      
   
        }

        wx.previewImage({
            current: url,
            //数据源
            urls: arr
        });
          }
       
       
    },
    //获取手机壁纸
    get_image_phone(page, type) {
        let that = this
        wx.showLoading({
            title: '加载中',
            mask: true
        })
        wx.request({
            url: 'https://www.xiaoyankai.com/mini/nologin/api/photoListByClassify',
            method: 'GET',
            data: {
                classifyId: type,
                paggNo: page
            },
            success(res) {

                that.setData({
                    image_list: [...that.data.image_list, ...res.data.data ]
                })
                wx.hideLoading()
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onShow: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
     onLoad: function () {
        // this.get_avatar(1,1)
        get_show().then(res => {
        
            if (res.data.password == app.getVersion()) {
                this.data.type_6 = res.data.stuNumber
             
              }else{
                this.data.type_6 = 'false'
              }
         
            this.setData({
                type_6: this.data.type_6
              })
          })
        if (this.data.type_shebei == 1) {
            // this.data.type=26
            // this.get_pc_img(this.data.type)
            this.setData({
                type_1: this.data.type_2
            })

        } else {
            this.setData({
                type_1: this.data.type_3
            })
            this.get_image_phone(this.data.page, this.data.type)
        }

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
      
        // wx.vibrateShort({
        //     type:'heavy',
        //     success: res => {
        //       console.log(res)
        //     }
        //   })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.data.page++
        if(this.data.type_shebei==2){
            this.get_image_phone(this.data.page, this.data.type )
        }else if(this.data.type_shebei==3){
            this.get_avatar(this.data.page,this.data.type)
        }
        

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})